package com.snowleopard.findpeople.web.publish;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.snowleopard.findpeople.entity.publish.ImageStorage;
import com.snowleopard.findpeople.service.publish.ImageStorageManager;
import com.snowleopard.findpeople.web.CrudActionSupport;

@Namespace("/publish")
@Results({ @Result(name = "publish-info", location = "publish-info!input.action?infoPublishId=${infoPublishId}", type = "redirect") })
public class ImageStorageAction extends CrudActionSupport<ImageStorage> {
    private static final long serialVersionUID = -1354236915065520539L;

    private static final Logger log = LoggerFactory.getLogger(ImageStorageAction.class);

    @Autowired
    private transient ImageStorageManager imageStorageManager;

    private String infoPublishId;
    private String imageStorageId;
    private File file;
    private String fileFileName;

    @Override
    public ImageStorage getModel() {
        // TODO Auto-generated method stub
        return null;
    }

    @Action(value = "startUploadFile", interceptorRefs = @InterceptorRef("fileUploadStack"))
    public String startUploadFile() throws Exception {
        log.info("begin upload file.... ");
        FileInputStream fis = null;
        byte[] buffer = null;

        try {
            fis = new FileInputStream(file);
            int length = fis.available();

            buffer = new byte[length];
            BufferedInputStream bis = new BufferedInputStream(fis);
            bis.read(buffer, 0, length);

            if (length > 0 && StringUtils.isNotBlank(fileFileName)) {
                ImageStorage imageStorage = new ImageStorage();

                imageStorage.setPicture(buffer);
                imageStorage.setName(fileFileName);
                imageStorageManager.save(imageStorage, infoPublishId);

            }
        } catch (Exception e) {
            throw e;
        } finally {
            if (null != fis)
                fis.close();
        }

        return "publish-info";
    }

    public String viewOne() throws Exception {

        ImageStorage imageStorage = imageStorageManager.get(imageStorageId);
        HttpServletResponse res = Struts2Utils.getResponse();
        res.setContentType("image/JPEG;charset=UTF-8");

        ServletOutputStream os = null;
        try {
            os = res.getOutputStream();
            os.write(imageStorage.getPicture());

        } catch (Exception e) {
            throw e;
        } finally {
            if (os != null) {
                os.flush();
                os.close();
            }
        }

        return null;
    }

    @Override
    public String list() throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String input() throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String save() throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String delete() throws Exception {
        imageStorageManager.delete(imageStorageId);
        return "publish-info";
    }

    public String getImageStorageId() {
        return imageStorageId;
    }

    public void setImageStorageId(String imageStorageId) {
        this.imageStorageId = imageStorageId;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getInfoPublishId() {
        return infoPublishId;
    }

    public void setInfoPublishId(String infoPublishId) {
        this.infoPublishId = infoPublishId;
    }

    public String getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

}
